package
{
import com.adobe.nativeExtensions.AdBanner;
import com.adobe.nativeExtensions.AdBannerEvent;
import com.adobe.nativeExtensions.AdBannerPosition;
import com.devilishgames.air.extensions.admob.ios.AdMob;

import fl.controls.Button;

import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.StageVideoAvailabilityEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.SoundTransform;
import flash.media.StageVideo;
import flash.media.StageVideoAvailability;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.text.TextField;
import flash.utils.setTimeout;

public dynamic class TestIPadVideo extends Sprite
{
	private var tf:TextField;
	private var video:StageVideo;
	private var nc:NetConnection;
	public function TestIPadVideo()
	{
		NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
		NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true);
		
		log("init");
		
		stage.align = StageAlign.TOP_LEFT;
		stage.scaleMode = StageScaleMode.NO_SCALE ;
		stage.addEventListener( StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY , stageVideoState );
		
		var btn:Button = new Button();
		btn.label = "缩小";
		btn.addEventListener(MouseEvent.CLICK, btnMouseClickHandler);
		btn.x = btn.y = 200; 
		
		addChild(btn);
		
		addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
	}
	
	private function addedToStageHandler(event:Event):void
	{
		removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		setTimeout(initAD, 2000);
	}
	
	private var adMob:AdMob;
	private function initAD():void
	{
		
		try {
			log("adMob init");
			adMob = new AdMob(); // Instanciamos la clase AdMob
			//			adMob.id = "ID DE ADMOB"; // Indicar el ID de la APP de AdMob
			adMob.size = AdMob.GAD_SIZE_728x90; // Indicamos el tamaño del Banner
			adMob.position = new Point(0, stage.stageHeight - adMob.size.y); // Indicamos la posición del banner
			adMob.testMode = true; // Decimos que no use el modo test
			adMob.showAdMob(); // Mostramos el banner
		} catch (e:Error) {
			log("error: " + e.getStackTrace());
		}
		
		AdBanner.adView.startShowingAds(AdBannerPosition.TOP, false);  
		AdBanner.adView.addEventListener(AdBannerEvent.AD_LOADED, adLoaded);  
		AdBanner.adView.addEventListener(AdBannerEvent.AD_LOADING_FAILED, adFailed)
		AdBanner.adView.addEventListener(AdBannerEvent.USER_INTERACTION_STARTING, adStart);  
		AdBanner.adView.addEventListener(AdBannerEvent.USER_INTERACTION_FINISHED, adFinished); 
		
	}
	
	private function adLoaded(event:AdBannerEvent):void
	{
		log("Ad Loaded");         
		AdBanner.adView.visible = true;         
		log("leaving: " + event.leaving); 
	}
	
	private function adFailed(event:AdBannerEvent):void
	{
		log("Loaded Failded: " + event.errorCode);
		AdBanner.adView.visible = false;
	}
	
	private function adStart(event:AdBannerEvent):void
	{
		log("Starting");
		log(stage.orientation);
	}
	
	private function adFinished(event:AdBannerEvent):void
	{
		log("Finished");
		log(stage.orientation);
	}
	
	
	
	
	protected var stream:NetStream ;
	
	private function btnMouseClickHandler(event:MouseEvent):void
	{
		if (video)
		{
			video.viewPort = new Rectangle(0, 0, 500, 420);
		}
	}
	
	protected function stageVideoState( e:StageVideoAvailabilityEvent ):void
	{
		
		var available:Boolean = e.availability == StageVideoAvailability.AVAILABLE ;
		
		log("stageVideoState: " + available);
		if ( available )
		{
			nc = new NetConnection() ;
			nc.client = {};
			nc.addEventListener(NetStatusEvent.NET_STATUS, connHandler);
			nc.connect(null, "nalidilian") ;
			//			nc.connect("rtmp://192.168.3.196/liveorigin", "nalidilian") ;
			//			stream.play( "http://192.168.3.84/static/video/sample.mp4") ;
		}
		
	}
	
	private function connHandler(event:NetStatusEvent):void
	{
		log(event.info.code);
		if (event.info.code == "NetConnection.Connect.Success")
		{
			stream = new NetStream(nc) ;
			stream.addEventListener(NetStatusEvent.NET_STATUS, function(event:NetStatusEvent):void
			{
				log(event.info.code);
			});
			stream.client = this ;
			video = stage.stageVideos[0] ;
			video.viewPort = new Rectangle( 0, 0, 1280 , 720 ) ;
			video.attachNetStream( stream ) ;
			stream.soundTransform = new SoundTransform(1);
			//			stream.play( "http://192.168.3.196:1935/liveedge/mp4:aa_ios.mp4/manifest.f4m") ;
			/*stream.play( "http://192.168.3.196:1935/liveorigin/mp4:d_ios.mp4/playlist.m3u8") ;*/
			stream.play( "http://192.168.3.84/static/video/sample.mp4") ;
		}
	}
	
	private function log(msg:String):void
	{
		if (!tf)
		{
			tf = new TextField();
			tf.multiline = tf.wordWrap = true;
			tf.width = 300;
			tf.x = tf.y = 100;
			addChild(tf);
		}
		trace(msg);
		tf.appendText(msg + "\n");
	}
	
	public function onMetaData( info:Object ):void
	{
		
	}
	
	private function handleActivate(event:Event):void
	{
		NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
	}
	
	private function handleDeactivate(event:Event):void
	{
		NativeApplication.nativeApplication.exit();
	}			
	
}
}

//package
//{
//import fl.controls.Button;
//
//import flash.display.Sprite;
//import flash.display.StageAlign;
//import flash.display.StageScaleMode;
//import flash.events.MouseEvent;
//import flash.events.SampleDataEvent;
//import flash.events.StageVideoAvailabilityEvent;
//import flash.geom.Rectangle;
//import flash.media.Microphone;
//import flash.media.Sound;
//import flash.media.StageVideo;
//import flash.media.StageVideoAvailability;
//import flash.net.NetConnection;
//import flash.net.NetStream;
//import flash.text.TextField;
//import flash.utils.ByteArray;
//
///**
// * 
// * @author waitingsnow
// * $Id: TestIPadVideo.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
// */
//public class TestMobileAs extends Sprite
//{
//	
//	private var playVideo:PlayVideo;
//	private var recordBtn:Button;
//	private var stopRecordBtn:Button;
//	private var playBtn:Button;
//	private var micro:Microphone;
//	private var soundBytes:ByteArray = new ByteArray();
//	private var sound:Sound;
//	private var tf:TextField;
//	
//	public function TestMobileAs()
//	{
//		super();
//		
//		// support autoOrients
//		stage.align = StageAlign.TOP_LEFT;
//		stage.scaleMode = StageScaleMode.NO_SCALE;
//		
//		/*recordBtn = new Button();
//		recordBtn.addEventListener(MouseEvent.CLICK, recordBtnClickHandler);
//		recordBtn.label = "录制";
//		addChild(recordBtn);
//		playBtn = new Button();
//		playBtn.addEventListener(MouseEvent.CLICK, playBtnClickHandler);
//		playBtn.label = "播放";
//		playBtn.x = recordBtn.width + 5;
//		addChild(playBtn);
//		playBtn.y = recordBtn.y = 10;*/
//		
//		var output:LogOutput = new LogOutput();
//		addChild(output);
//		output.activate();
//		
//		playVideo = new PlayVideo();
////		playVideo.url = "rtmp://video.9nali.com/nali";
//		playVideo.url = null;
////		playVideo.startPlay("http://192.168.3.84/static/video/sample.mp4");
//		playVideo.startPlay("http://192.168.3.84/static/video/aa_ios.mp4");
//		playVideo.setSize(320, 240);
//		addChild(playVideo);
//		trace("sssdd");
//		
//	}
//	
//	private function log(msg:String):void
//	{
//		if (!tf)
//		{
//			tf = new TextField();
//			tf.multiline = tf.wordWrap = true;
//			tf.width = 300;
//			tf.x = tf.y = 100;
//			addChild(tf);
//		}
//		tf.appendText(msg + "\n");
//	}
//	
//	private function playBtnClickHandler(event:MouseEvent):void
//	{
//		if (soundBytes && soundBytes.length)
//		{
//			soundBytes.position = 0;
//			sound = new Sound();
//			sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackHandler);
//			sound.play();
//		}
//	}
//	
//	private function playbackHandler(event:SampleDataEvent):void
//	{
//		var sample:Number;
//		for (var i:int = 0; i < 8192; i++) 
//		{
//			if (soundBytes.bytesAvailable > 0) 
//			{
//				sample = soundBytes.readFloat();
//				event.data.writeFloat(sample); // channel left
//				event.data.writeFloat(sample); // channel right
//			}
//		}
//	}
//	
//	private function recordBtnClickHandler(event:MouseEvent):void
//	{
//		var flag:Boolean = (recordBtn.label == "录制");
//		recordBtn.label = flag ? "停止录制" : "录制";
//		
//		log("recordBtn.label: " + recordBtn.label);
//		log("Microphone.isSupported: " + Microphone.isSupported);
//		
//		if (!micro)
//		{
//			micro = Microphone.getMicrophone();
//			micro.gain = 60;
//			micro.rate = 44;
//			micro.setSilenceLevel(0, 2000);
//		}
//		if (flag)
//		{
//			/*sound && sound.close();*/
//			soundBytes.clear();
//			micro.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleDataHandler);
//		}
//		else
//		{
//			/*soundBytes.clear();*/
//			micro.removeEventListener(SampleDataEvent.SAMPLE_DATA, sampleDataHandler);
//		}
//	}
//	
//	private function sampleDataHandler(event:SampleDataEvent):void
//	{
//		while(event.data.bytesAvailable) 
//		{
//			var sample:Number = event.data.readFloat();
//			soundBytes.writeFloat(sample);
//		}
//	}
//}
//}